import {
  detailsPayment,
  payment,
  obtain_openid,
  cancelOrder
} from '../../../api.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: '', //定时器名字
    countDownNum: '300', //倒计时初始值
    full_name: '',
    pic_url: '',
    price: '',
    unitOrdersQuery: '',
    batchcode: '',
    lastPge: '',
    pay_index: -1,
    pay_length: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      lastPge: options.type
    })
    detailsPayment({
      token: app.globalData.userInfo.token,
      batchcode: options.order
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        if (res.data.data.orderInfoData[0].price < 10000) {
          this.invokeWxPay(res.data.data.orderInfoData[0].batchcode, res.data.data.orderInfoData[0].price)
        }
        that.setData({
          full_name: res.data.data.orderInfoData[0].full_name,
          pic_url: res.data.data.orderInfoData[0].pic_url,
          price: res.data.data.orderInfoData[0].price,
          unitOrdersQuery: res.data.data.unitOrdersQuery,
          batchcode: res.data.data.orderInfoData[0].batchcode,
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
    this.countDown();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum; //获取倒计时初始值
    that.setData({
      timer: setInterval(function () {
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          cancelOrder({
            token: app.globalData.userInfo.token,
            batchcode: that.data.batchcode
          }).then(res => {
            if (res.data.code == 666) {
              wx.showToast({
                title: '订单已取消',
                icon: 'none'
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          })
        }
      }, 1000)
    })
  },
  payforOrder: function (e) {
    var total_fee = e.target.dataset.total
    var batchcode = e.target.dataset.code
    var pay_index = e.target.dataset.index
    var that = this
    that.setData({
      pay_index: pay_index
    })
    that.invokeWxPay(batchcode, total_fee)
  },
  invokeWxPay(batchcode, total_fee) {
    var that = this
    payment({
      token: app.globalData.userInfo.token,
      batchcode: batchcode,
      openid: app.globalData.userInfo.user.openid,
      total_fee: total_fee
    }).then(res => {
      app.globalData.hide_num = 1
      console.log(res.data)
      var jsonStr = res.data;
      jsonStr = jsonStr.replace(" ", "");
      jsonStr = jsonStr.replace(/\ufeff/g, "");
      var requ_json = JSON.parse(jsonStr);
      console.log(requ_json)
      wx.requestPayment({
        timeStamp: requ_json.timeStamp,
        nonceStr: requ_json.nonceStr,
        package: requ_json.package,
        signType: requ_json.signType,
        paySign: requ_json.paySign,
        success(res) {
          if (that.data.pay_index == -1) {
            if (that.data.lastPge == 1) {
              wx.reLaunch({
                url: '/pages/goodControl/goodControl'
              })
            }
            if (that.data.lastPge == 2) {
              wx.redirectTo({
                url: '/pages/user/myOrder/myOrder'
              })
            }
            if (that.data.lastPge == 3) {
              wx.redirectTo({
                url: '/pages/user/myOrder/myOrder'
              })
            }
          } else {
            that.data.unitOrdersQuery[that.data.pay_index] = 1;
            that.setData({
              unitOrdersQuery: that.data.unitOrdersQuery,
              pay_length: that.data.pay_length + 1
            })
            if (that.data.pay_length == that.data.unitOrdersQuery.length) {
              if (that.data.lastPge == 1) {
                wx.reLaunch({
                  url: '/pages/goodControl/goodControl'
                })
              }
              if (that.data.lastPge == 2) {
                wx.redirectTo({
                  url: '/pages/user/myOrder/myOrder'
                })
              }
              if (that.data.lastPge == 3) {
                wx.redirectTo({
                  url: '/pages/user/myOrder/myOrder'
                })
              }
            }
          }
        }
      })
    })
  }
})